package com.xx.java8.chapter1;

import com.xx.java8.domain.Apple;
import com.xx.java8.enums.ColorEnum;
import com.xx.java8.utils.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

public class App {


    public static void main(String[] args) {
        List<Apple> appleList = new ArrayList<Apple>();
        Apple apple;
        for (int i = 0; i < 10; i++) {
            apple = new Apple();
            apple.setWeight(Math.random() * 53);
            apple.setColor(i % 2 == 0 ? ColorEnum.GREEN : ColorEnum.RED);
            appleList.add(apple);
        }
        System.out.println(appleList);
//        Collections.sort(appleList, new Comparator<Apple>() {
//            @Override
//            public int compare(Apple o1, Apple o2) {
//                return o1.getWeight().compareTo(o2.getWeight());
//            }
//        });
        appleList.sort(Comparator.comparing(Apple::getWeight));
        System.out.println(appleList);

        System.out.println("绿色苹果");
        System.out.println(Apple.filter(appleList, Apple::isGreen));
        System.out.println("红色苹果");
        System.out.println(Apple.filter(appleList, apple1 -> apple1.getColor().getName().equals(ColorEnum.RED.getName())));

        System.out.println("苹果重量>=20,且绿色");
        System.out.println(CollectionUtils.filter(appleList, a -> a.getWeight() >= 20 && a.isGreen()));

        System.out.println("绿色苹果 stream");
        System.out.println(appleList.stream()
                .filter(a -> a.isGreen())
                .collect(Collectors.toList()));

        System.out.println("绿色苹果 parallelStream");
        System.out.println(appleList.parallelStream()
                .filter(a -> a.isGreen())
                .collect(Collectors.toList()));

//        File[] files = new File(".").listFiles(new FileFilter() {
//            @Override
//            public boolean accept(File file) {
//                return file.isHidden();
//            }
//        });

//        File[] files = new File(".").listFiles(file -> file.isHidden());

//        File[] files = new File(".").listFiles(File::isHidden);
    }
}
